<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员手机" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入会员手机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="请选择"
          end-placeholder="请选择"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="search(0)"
          >待兑现</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="search(1)"
          >已兑现</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="postList">
      <el-table-column label="订单编号" align="center" prop="orderCode" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      />
      <el-table-column label="会员昵称" align="center" prop="memberName" />
      <el-table-column label="会员手机" align="center" prop="phonenumber" />
      <el-table-column label="权益名称" align="center" prop="raiName" />
      <el-table-column label="数量" align="center" prop="raiOrderNum">
        <template slot-scope="scope">
          {{ formatTableValue(scope.row.raiOrderNum) }}
        </template>
      </el-table-column>
      <el-table-column label="领取方式" align="center" prop="payment">
        <template slot-scope="scope">
          {{ scope.row.payment == 0 ? "快递寄送" : "门店" }}
        </template>
      </el-table-column>
      <el-table-column label="门店" align="center" prop="storeName">
        <template slot-scope="scope">
          {{ formatTableValue(scope.row.storeName) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          {{ scope.row.raiStatus == 0 ? "待兑现" : "已兑现" }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" :disabled="scope.row.raiStatus == 1" @click="sendOutGoods(scope.row)">
            发货</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="快递寄送"
      :visible.sync="open"
      width="500px"
      append-to-body :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="formAddressName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="formTel" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="formAddressDetail" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="快递公司" prop="expressCompany">
          <el-select v-model="form.expressCompany" placeholder="请选择快递公司">
            <el-option
              v-for="dict in dict.type.express_company"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号" prop="expressCode">
          <el-input
            v-model="form.expressCode"
            max-length="20"
            placeholder="请输入快递单号"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('form')"
          >确认提交</el-button
        >
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="门店自提"
      :visible.sync="open2"
      width="500px"
      append-to-body :close-on-click-modal="false"
    >
      <el-form ref="form2" :model="form2" label-width="80px">
        <el-form-item label="门店名称">
          <el-select
            v-model="form2.storeId"
            filterable
            clearable
            placeholder="请选择门店"
            @change="getStoreId"
          >
            <el-option
              v-for="item in expressWayOptions"
              :key="item.id"
              :label="item.storeName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="storeAddress">
          <el-input v-model="form2.storeAddress" placeholder="请输入地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2('form2')"
          >确认兑换</el-button
        >
        <el-button @click="open2 = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getOrderListAPI,
  addExpressAPI,
  getAddressAPI,
} from "@/api/system/member";
import { getStoreList } from "@/api/system/store";
import { formatDate } from "@/utils/index";
export default {
  name: "membershipBenefitsOrder",
  dicts: ["express_company"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 1,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        orderCode: undefined,
        phonenumber: undefined,
        startDate: undefined,
        endDate: undefined,
      },
      expressWayOptions: [],
      dateRange: [],
      form: {},
      form2: {},
      rules: {
        expressCode: [
          {
            required: true,
            message: "快递单号必填，限输入20个字符",
            trigger: "blur",
          },
        ],
      },
      open2: false,
      orderIdraiId: {},
      formAddressName: "",
      formTel: "",
      formAddressDetail: "",
    };
  },
  watch: {
    dateRange: {
      handler(newVal, oldVal) {
        if (newVal != null && newVal.length > 0) {
          this.queryParams.startDate = newVal[0];
          this.queryParams.endDate = newVal[1];
        } else {
          this.queryParams.startDate = undefined;
          this.queryParams.endDate = undefined;
        }
      },
    },
  },
  created() {
    this.getList();
    this.getMd();
  },
  methods: {
    getStoreId() {
      this.form2.storeAddress =
        this.expressWayOptions.filter((i) => i.id == this.form2.storeId)[0]
          .storeAddress || "";
    },
    getAddress(row) {
      getAddressAPI(row.addressId).then((response) => {
        if (response.code == 200) {
          this.formAddressName = response.data.addressName;
          this.formTel = response.data.tel;
          this.formAddressDetail = response.data.addressDetail;
        }
      });
    },
    getMd() {
      getStoreList({
        pageNum: 1,
        pageSize: 10000,
      }).then((response) => {
        if (response.code == 200) {
          this.expressWayOptions = response.rows;
        }
      });
    },
    search(status) {
      this.queryParams.raiStatus = status;
      this.getList();
    },
    getList() {
      this.loading = true;
      getOrderListAPI(this.queryParams).then((response) => {
        if (response.code == 200) {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    sendOutGoods(row) {
      console.log("row: 11", row);
      this.orderIdraiId = {
        orderId: row.orderCode,
        raiId: row.raiId,
        payment: row.payment,
        id: row.id,
        goodsId: row.goodsId,
      };
      if (row.payment == 0) {
        this.orderIdraiId.expressWay = 1;
        this.open = true;
        this.getAddress(row);
      } else {
        this.form2 = {
          storeId: row.storeId,
          expressWay: 0,
        };
        this.form2.storeAddress =
          this.expressWayOptions.filter((i) => i.id == row.storeId)[0]
            .storeAddress || "";
        this.open2 = true;
      }
    },
    /** 提交按钮 */
    submitForm(formName) {
      this.$refs[formName].validate(async (valid, errorObj) => {
        if (valid) {
          addExpressAPI({ ...this.form, ...this.orderIdraiId }).then(
            (response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("成功");
                this.open = false;
                this.open2 = false;
                this.getList();
              }
            }
          );
        }
      });
    },
    /** 提交按钮 */
    submitForm2(formName) {
      this.$refs[formName].validate(async (valid, errorObj) => {
        if (valid) {
          addExpressAPI({ ...this.form2, ...this.orderIdraiId }).then(
            (response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("成功");
                this.open = false;
                this.open2 = false;
                this.getList();
              }
            }
          );
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/order/export",
        {
          ...this.queryParams,
        },
        `会员权益订单_${formatDate(new Date())}.xlsx`
      );
    },
  },
};
</script>
